package com.soil.code.generation.domain.mybatis.service;

import com.soil.code.generation.domain.mybatis.pojo.MybatisCodeGenerationInfo;
import com.soil.code.generation.domain.pojo.POClassModel;
import com.soil.code.generation.domain.pojo.POFieldModel;
import com.soil.code.generation.infrastructure.AbstractFreeMakerCodeGenerator;
import com.soil.code.generation.infrastructure.util.NamingCaseUtil;
import com.ssy.gaea.common.collection.CollectionUtils;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.List;

/**
 * @author zeno
 */
@Component
public class MybatisCodeGenerator extends AbstractFreeMakerCodeGenerator{

  public static final String MAPPER_CLASS_FTL = "MapperClass.java.ftl";

  public static final String PO_CLASS_FTL = "PersistenceObject.java.ftl";


  public MybatisCodeGenerator() throws IOException{
    super("classpath*:/code-templates/mybatis-mapper");
  }

  public void generate( MybatisCodeGenerationInfo generationInfo ) throws IOException, TemplateException{
    generatePOClass(generationInfo);
  }



  private void generatePOClass( MybatisCodeGenerationInfo generationInfo ) throws IOException, TemplateException{

    POClassModel templateInfo = createPODataModel(generationInfo);

    Template template = templateConfig.getTemplate(PO_CLASS_FTL);
    Writer out = new OutputStreamWriter(System.out);
    templateInfo.setPkg(generationInfo.getPkg());
    templateInfo.setAuthor(generationInfo.getAuthor());
    template.process(templateInfo,out);
  }

  private POClassModel createPODataModel( MybatisCodeGenerationInfo generationInfo ){

    POClassModel templateInfo = new POClassModel();

    MybatisCodeGenerationInfo.TableInfo tableInfo = generationInfo.getTableInfo();
    templateInfo.setTableName(tableInfo.getName());
    templateInfo.setClassName(NamingCaseUtil.toUpperCamelCase(tableInfo.getName()));
    templateInfo.setTableComment(tableInfo.getComment());

    List<POFieldModel> fieldInfos =
      CollectionUtils.convert(generationInfo.getColumnInfos(),this::toFieldInfo);
    templateInfo.setFieldInfos(fieldInfos);
    return templateInfo;
  }

  private POFieldModel toFieldInfo( MybatisCodeGenerationInfo.ColumnInfo columnInfo ){

    POFieldModel field = new POFieldModel();

    String name = columnInfo.getName();

    field.setJavaType(columnInfo.getJavaType());
    field.setColumnName(name);
    field.setIsPrimaryKey(columnInfo.getIsPrimaryKey());
    field.setIsAutoIncrement(columnInfo.getIsAutoIncrement());
    field.setComment(columnInfo.getComment());

    if(name.startsWith("_")){
      name = name.substring(1);
    }
    field.setName(NamingCaseUtil.toLowerCamelCase(name));
    field.setConstantColumnName(NamingCaseUtil.toUpperSnakeCase(name));

    return field;
  }

//
//  /**
//   * @author zeno
//   */
//  @Data
//  private static class POClassModel{
//
//    /** 包路径 */
//    private String pkg;
//
//    /** 作者 */
//    private String author;
//
//    /** 表注释 */
//    private String tableComment;
//
//    /** 表名 */
//    private String tableName;
//
//    /** 类名 */
//    private String className;
//
//    /** 映射类名称 */
//    private String mapperName;
//
//    /** 持久对象字段 */
//    private List<POFieldModel> fieldInfos;
//  }
//
//  /**
//   * 持久对象字段信息
//   * @author zeno
//   */
//  @Data
//  private static class POFieldModel{
//
//    /** 字段类型 */
//    private String javaType;
//
//    /** 字段名 */
//    private String name;
//
//    /** 列名 */
//    private String columnName;
//
//    /** 常量类名 */
//    private String constantColumnName;
//
//    /** 是否主键 */
//    private Boolean isPrimaryKey;
//
//    /** 是否自增字段 */
//    private Boolean isAutoIncrement;
//
//    /** 字段注释 */
//    private String comment;
//  }
}
